package com.ruoyi.system.service.sso;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jayway.jsonpath.JsonPath;
import com.ruoyi.system.domain.IndsvrSsoApp;
import com.ruoyi.system.domain.IndsvrSsoAppUserMap;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class SunPanel  implements SimulationLogin{
    @Override
    public Object run(IndsvrSsoApp app, IndsvrSsoAppUserMap user, HttpServletRequest request) throws Exception {
        return run(user.getUsername(),user.getPassword(),app.getBaseUrl());
    }

    public Auths run(String user, String pwd, String baseUrl) throws Exception{


        OkHttpUtils okHttpUtils = new OkHttpUtils();
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("username",user);
        stringStringHashMap.put("password",pwd);
        String s = okHttpUtils.postForJson(baseUrl+"/api/login", new Gson().toJson(stringStringHashMap));

        String token = JsonPath.read(s, "$.data.token");
//        String read = JsonPath.read(s, "$.data");

        okHttpUtils.putHead("Token",token);

//        String s1 = okHttpUtils.postForJson("https://www.indsvr.com/api/panel/itemIconGroup/getList", "{}");
//
//        Object read = JsonPath.read(s1, "$.data.user");

        HashMap<String, Object> value = new HashMap<>();

        value.put("token",token);
        value.put("userInfo","{}");
        value.put("visitMode",0);

        Auths auths = new Auths();

        Token token1 = new Token();
        token1.data=value;
        Gson gson = new GsonBuilder().serializeNulls().create();
        auths.token= gson.toJson(token1);
        return auths;
    }


    @Data
    public static class Auths{
        String token;
    }

    @Data
    public static class Token{
        private Object data;
        private String expire = null;

    }
}
